﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace Twodi.Script
{
    public partial class ScriptForm : Form
    {
        /// <summary>
        /// Virtual Machine acoplada ao form.
        /// </summary>
        public LuaVirtualMachine VirtualMachine { get; set; }

        public ScriptForm(LuaVirtualMachine virtualMachine)
        {
            this.VirtualMachine = virtualMachine;
            InitializeComponent();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            ClearErrors();
            SendCommand();
        }

        protected void ClearErrors()
        {
            lblResults.Text = "";
        }

        protected void ClearScript()
        {
            txtScript.Text = "";
        }

        /// <summary>
        /// Enviar comando para o objeto comandado.
        /// </summary>
        protected void SendCommand()
        {
            this.Refresh(VirtualMachine.Run(txtScript.Text));
        }

        /// <summary>
        /// Atualizar forms com o valor do resultado.
        /// </summary>
        public void Refresh(object[] results)
        {
            if (results == null) return;
            for (int i = 0; i < results.Length; i++)
            {
                if (results[i] != null) lblResults.Text += results[i].ToString();
            }
            base.Refresh();
        }
    }
}
